package com.rabbitmq.send;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.config.RabbitMQDirectConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * direct模式生产者
 */
@Component
@Slf4j
public class DirectSender
{

	@Autowired
	private AmqpTemplate amqpTemplate;

	public void sendDirect(JSONObject obj)
	{
		Message message = MessageBuilder.withBody(obj.toJSONString().getBytes())
			.setContentType(MessageProperties.CONTENT_TYPE_JSON)
			.setMessageId("123")
			.setHeader("bar", "baz")
			.build();
		this.amqpTemplate.convertAndSend(RabbitMQDirectConfig.DIRECT_QUEUE, message);
	}
}
